//JavaScript
var curentArticleId;

/**
 * MÚthodes d'envoi de requÛtes
 */ 

function getArticle(idArticle){
  if (idArticle != -1){
    var url = 'include/bd/update_article_bd.php';
    $.getJSON(url
        ,{type_requete:'get_article'
          ,id_art:idArticle}
        ,gestionReponseArticle);
  }else{
    gestionReponseArticle(null);  
  }
}

function getSections(){
  var url = 'include/bd/update_article_bd.php';
  $.getJSON(url
    ,{type_requete:'get_sections'}
    ,gestionReponseSection);
}

function validArticle(){
// On valide l'article en base
 var url = 'include/bd/update_article_bd.php';
 var typeUpdate = 'add_article';
 if (curentArticleId != -1){
    typeUpdate = 'edit_article';
 }
 var sectionArticle = $('#idSections')[0].value;
 var titreArticle = $('#idTitreArticle')[0].value;
 var texteArticle = $('#idTexteArticle')[0].value;
 
 if (trim(titreArticle) == ""){
  alert("Vous devez mettre un titre.");
 }else{
   
   if (trim(texteArticle) == ""){
    alert("Vous devez mettre un descriptif d'article.");
   }else{
     titreArticle = htmlentities(titreArticle);
     texteArticle = htmlentities(texteArticle);
     // Ó la suite de l'appel on mettra Ó jour les listes d'illustrations
     $.getJSON(url
        ,{type_requete:'update_article'
          , type_update:typeUpdate
          , id_article:curentArticleId
          , section_article:sectionArticle
          , titre_article:titreArticle
          , texte_article:texteArticle}
        ,gestionReponseValidArticle);
    }
  }
}

function addSectionToBase(){
  var nomSection = $('#idNewSection')[0].value;
  var url = 'include/bd/update_article_bd.php';
  $.getJSON(url
    ,{type_requete:'add_section'
      ,nom_section:nomSection}
    ,gestionReponseAddSection);
}

/**
 * MÚthodes de gestion de la rÚcupÚration des requÛte
 */ 

function gestionReponseArticle(article)
{
    if (article != null){
      $('#idTitreArticle')[0].value = html_entity_decode(article.titre);
      $('#idTexteArticle').html(html_entity_decode(article.texte));
      if (article.illustrations.length > 0){
         setListIllustrations(article.illustrations);
      }
    }
}

function gestionReponseSection(sections){
  if (sections != null){
    for (var i = 0; i < sections.length; i++){
      var section = sections[i];
      addSection(section.idSection, section.nomSection);      
    }
    $("#idSections")[0].options[0].selected = true;
  }
}

function gestionReponseValidArticle(article){
  manageAddAndRemoveIllustrations(article.idArt, 'illustre_articles', 'id_art');
  // à la suite de l'appel on mettra à jour le flux rss
  /*
  var url = 'include/bd/rss_bd.php';   
  $.getJSON(url
      ,{type_requete:'update_rss'
        ,rss_description:'des articles de Roule Maboule'
        ,req_table:'article'
        ,req_table_ill:'illustre_articles'
        ,col_id:'id_art'
        ,col_titre:'titre'
        ,col_desc:'texte'
        ,col_date:'date'
        ,rss_page:'articles'
        ,rss_file:'articles-rss.xml'
        ,req_opt:''
        }
      ,gestionReponseRSS);
      */
  // On finit par changer de page
  changePage();
}

function gestionReponseRSS(section){
  alert(section);
}

function gestionReponseAddSection(section){
  //On vide le champ associÚ
  $('#idNewSection')[0].value = '';
  addSection(section.idSection, section.nomSection);
}

/**
 * MÚthodes d'Údition graphique
 */ 


function manageLayout(idArticle){
  initIllustration(idArticle, "zoneIllustration", "divIllustration");
  curentArticleId = idArticle;
  var urlImg = "images/";
  if (idArticle == -1){
    urlImg += "articles.png";
    $('#titreZoneArticle').html("Ajouter un article");
  }else{
    urlImg += "articles.png";
    $('#divSection').hide();
    $('#trSectionArticle').hide();
    $('#titreZoneArticle').html("Editer un article");
  }
  
  $('#idImgModule')[0].src = urlImg;
}

function addSection(idSection, sectionName){
  var length = $("#idSections")[0].options.length; 
  $("#idSections")[0].options[length]= 
        new Option(sectionName
                    ,idSection
                    , false
                    , true);
}

function changePage(){
  document.location = 'index.php?page=articles'; 
}
